package learnnet.jetty.simpleapplication;

import java.util.concurrent.Executors;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.handler.HandlerList;
import org.eclipse.jetty.server.handler.ResourceHandler;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.util.thread.ExecutorThreadPool;
import org.eclipse.jetty.util.thread.ThreadPool;

/**
 * @author qindongliang
 * 
 * 启动jetty测试服务
 * 
 * 
 * **/
public class StartupServer {
	public static void main(String[] args) throws Exception {
		ResourceHandler resourceHandler = new ResourceHandler();
		resourceHandler.setDirectoriesListed(true);
		resourceHandler.setResourceBase("D:/test");
		resourceHandler.setStylesheet("");

		ContextHandler staticContextHandler = new ContextHandler();
		staticContextHandler.setContextPath("/data");
		staticContextHandler.setHandler(resourceHandler);

		// 添加servlet支持
		ServletContextHandler servletContextHandler = new ServletContextHandler(
				ServletContextHandler.NO_SESSIONS);
		servletContextHandler.setContextPath("/");
		/** 可在下面依次添加多个servlet **/
		servletContextHandler.addServlet(
				new ServletHolder(new MyTestServlet()), "/show");
		// servletContextHandler.addServlet(new ServletHolder(new
		// SpuSimilarityServlet()), "/spu");
		// //servletContextHandler.addServlet(new ServletHolder(new
		// SpuResultServlet()), "/search"); //
		// servletContextHandler.addServlet(new ServletHolder(new
		// AdminServlet()), "/db");
		HandlerList handlers = new HandlerList();
		handlers.addHandler(servletContextHandler);
		handlers.addHandler(staticContextHandler);
		int hosts = 9999;
		// 端口号
		Server server = new Server(hosts);
		// 设置线程池
		ThreadPool threadPool = new ExecutorThreadPool(
				Executors.newFixedThreadPool(5));
		server.setThreadPool(threadPool);
		// 设置连接参数
		Connector connector = new SelectChannelConnector();
		// 设置监听端口
		connector.setPort(hosts);
		// 为服务设置连接器
		server.setConnectors(new Connector[] { connector });
		server.setHandler(handlers);
		server.start();
		server.join();
	}
}
